home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 310_02 / string.c < prev    next >
C/C++ Source or Header  |  1990-04-18  |  3KB  |  149 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         string creation and deletion
  5.         timothy a. budd, 10/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         Oregon State University
  21.         Corvallis, Oregon
  22.         97331
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "string.h"
  28.  
  29. int ca_str = 0;
  30. int ca_wal = 0;
  31.  
  32. /* walloc allocates a string containing the same chars as the arg */
  33.  
  34. # define WALLOCINITSIZE 1000
  35.  
  36. static char wtable[WALLOCINITSIZE];
  37. int wtop = 0;
  38.  
  39. char *walloc(val)
  40. char *val;
  41. {    char *p;
  42.     int  size;
  43.  
  44.     size = 1 + strlen(val);
  45.     if ((size < 40) && ((wtop + size) < WALLOCINITSIZE)) {
  46.         p = &wtable[wtop];
  47.         wtop += size;
  48.         }
  49.     else {
  50.         p = o_alloc((unsigned) size);
  51.         ca_wal++;
  52.         }
  53.     strcpy(p, val);
  54.     return(p);
  55. }
  56.  
  57. /*---------------------------------------*/
  58. extern class *ArrayedCollection;
  59. extern object *o_acollection;
  60.  
  61. static mstruct *fr_string = 0;
  62.  
  63. # define STRINITSIZE 50
  64.  
  65. static string st_init_table[STRINITSIZE];
  66.  
  67. str_init() {
  68.     string *p;
  69.     mstruct *new;
  70.     int i;
  71.  
  72.     for (p = st_init_table, i = 0; i < STRINITSIZE; i++, p++) {
  73.         new = (mstruct *) p;
  74.         new->mlink = fr_string;
  75.         fr_string = new;
  76.         }
  77. }
  78.  
  79. extern int started;
  80. static new_rstr(new)
  81. string *new;
  82. {
  83.     new->s_ref_count = 0;
  84.     new->s_size = STRINGSIZE;
  85.     if (! started)
  86.         sassign(new->s_super_obj, o_acollection);
  87.     else if (ArrayedCollection)
  88.         sassign(new->s_super_obj, new_inst(ArrayedCollection));
  89.     else
  90.         new->s_super_obj = (object *) 0;
  91. }
  92.  
  93. string *new_istr(text)
  94. char *text;
  95. {    register string *new;
  96.  
  97.     if (fr_string) {
  98.         new = (string *) fr_string;
  99.         fr_string = fr_string->mlink;
  100.         }
  101.     else {
  102.         ca_str++;
  103.         new = structalloc(string);
  104.         }
  105.  
  106.     new->s_value = text;
  107.     new_rstr(new);
  108.     return(new);
  109. }
  110.  
  111. # define STRLISTMAX 100
  112.  
  113. mstruct *frl_str[STRLISTMAX];
  114.  
  115. object *new_str(text)
  116. char *text;
  117. {    int size;
  118.     string *new;
  119.  
  120.     size = 1 + strlen(text);
  121.     if ((size < STRLISTMAX) && frl_str[size]) {
  122.         new = (string *) frl_str[size];
  123.         frl_str[size] = frl_str[size]->mlink;
  124.         strcpy(new->s_value, text);
  125.         new_rstr(new);
  126.         }
  127.     else {
  128.         new = new_istr(walloc(text));
  129.         }
  130.     return((object *) new);
  131. }
  132.  
  133. free_string(s)
  134. string *s;
  135. {    int size;
  136.  
  137.     if (s->s_super_obj)
  138.         obj_dec(s->s_super_obj);
  139.     size = 1 + strlen(s->s_value);
  140.     if (size < STRLISTMAX) {
  141.         ((mstruct *)s)->mlink = frl_str[size];
  142.         frl_str[size] = (mstruct *) s;
  143.         }
  144.     else {
  145.         ((mstruct *)s)->mlink = fr_string;
  146.         fr_string = (mstruct *) s;
  147.         }
  148. }
  149.